﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spring.Data.Generic;
using System.Data;
using Spring.Data.Common;

namespace Ordering
{
    public static class ShopDAO
    {
        #region field

        private static AdoTemplate template = AppConfigurer.AdoTemplate;
        private static Object lockObj = new Object();

        private static string strBase = "select distinct Shop_Id, Shop_Name, Shop_Address, "
            + "Shop_Phone_No, Shop_PostCode from Shop";

        #endregion

        #region internal methods

        internal static void AddShop(Shop obj)
        {
            //Check input parameter
            if (obj == null)
                throw new Exception("ShopDAO.AddShop: Invalid parameter");

            lock (lockObj)
            {
                //Generate Shop_Id
                string strSearch = "SELECT isnull(max(Shop_Id),'S00000000') FROM Shop ";
                string strMaxNo = template.ExecuteScalar(CommandType.Text, strSearch).ToString();
                int nNum = Int32.Parse(strMaxNo.Substring(1)) + 1;
                obj.Shop_Id = "S" + nNum.ToString("00000000");

                //Insert into table Shop
                string strCreate = "insert into Shop values(@ShopId, @ShopName, @ShopAddress,"
                + "@ShopPhoneNo,@ShopPostCode)";

                IDbParameters para = template.CreateDbParameters();
                para.Add("ShopId", DbType.String).Value = obj.Shop_Id;
                para.Add("ShopName", DbType.String).Value = obj.Shop_Name;
                para.Add("ShopAddress", DbType.String).Value = obj.Shop_Address.ToString();
                para.Add("ShopPhoneNo", DbType.String).Value = obj.Shop_Phone_No;
                para.Add("ShopPostCode", DbType.Int32).Value = obj.Shop_PostCode;

                template.ExecuteNonQuery(CommandType.Text, strCreate, para);
            }
        }

        #endregion

        #region public

        public static Shop GetShop(string shopId)
        {
            if (string.IsNullOrEmpty(shopId))
            {
                throw new Exception("ShopDAO.GetShop: Invalid parameter");
            }
            string strSql = strBase + " where Shop_Id=@Shop_Id";

            IDbParameters para = template.CreateDbParameters();
            para.Add("Shop_Id", DbType.String).Value = shopId;

            IList<Shop> lst = template.QueryWithRowMapperDelegate(CommandType.Text, strSql,
                new RowMapperDelegate<Shop>(MapRow), para);

            return lst.Count > 0 ? lst[0] : null;
        }

        public static IList<Shop> ListShops()
        {
            return template.QueryWithRowMapperDelegate(CommandType.Text, strBase,
                new RowMapperDelegate<Shop>(MapRow));
        }

        //Check if the ShopName exists,return the number
        public static bool IsShopNameExist(string shopName)
        {
            //Check input parameter
            if (string.IsNullOrEmpty(shopName))
                throw new Exception("ShopDAO.ChectShopName: Invalid parameter");

            string strSql = "select count(*) from Shop where Shop_Name=@ShopName";
            IDbParameters p = template.CreateDbParameters();
            p.Add("ShopName", DbType.String).Value = shopName;

            int count = (int)template.ExecuteScalar(CommandType.Text, strSql, p);
            return count == 0 ? false : true;

        }

        #endregion

        #region private

        private static Shop MapRow(IDataReader dr, int rownumber)
        {
            Shop obj = new Shop();
            obj.Shop_Id = dr.IsDBNull(0) ? "" : dr.GetString(0);
            obj.Shop_Name = dr.IsDBNull(0) ? "" : dr.GetString(1);
            obj.Shop_Address = dr.IsDBNull(0) ? "" : dr.GetString(2);
            obj.Shop_Phone_No = dr.IsDBNull(0) ? "" : dr.GetString(3);
            obj.Shop_PostCode = dr.IsDBNull(0) ? 0 : dr.GetInt32(4);
            return obj;
        }

        #endregion
    }
}
